package com.suitsoft.ut.service.account;

import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.GrantedAuthorityImpl;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.transaction.annotation.Transactional;

import com.google.common.collect.Sets;
import com.suitsoft.ut.commons.LoginUser;
import com.suitsoft.ut.dao.account.UserDao;
import com.suitsoft.ut.entity.account.Authority;
import com.suitsoft.ut.entity.account.Role;
import com.suitsoft.ut.entity.account.User;
import com.suitsoft.ut.entity.system.TimeAuthority;

/**
 * 实现SpringSecurity的UserDetailsService接口,实现获取用户Detail信息的回调函数.
 * 
 * @author calvin
 */
@Transactional(readOnly = true)
public class UserDetailsServiceImpl implements UserDetailsService {

	private AccountManager accountManager;
	@Autowired
	private UserDao userDao;

	/**
	 * 获取用户Details信息的回调函数.
	 */
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException, DataAccessException {

		User user = accountManager.findUserByLoginName(username);
		if (user == null) {
			throw new UsernameNotFoundException("用户" + username + " 不存在");
		}

		Set<GrantedAuthority> grantedAuths = obtainGrantedAuthorities(user);

		//-- bdms示例中无以下属性, 暂时全部设为true. --//
		boolean enabled = true;
		boolean accountNonExpired = true;
		boolean credentialsNonExpired = true;
		boolean accountNonLocked = true;

//		UserDetails userdetails = new org.springframework.security.core.userdetails.User(user.getLoginName(), user
//				.getPassword(), enabled, accountNonExpired, credentialsNonExpired, accountNonLocked, grantedAuths);
		

		LoginUser userdetails = new LoginUser(user.getLoginName(), user.getPassword(), enabled, accountNonExpired, credentialsNonExpired, accountNonLocked, grantedAuths);
		//Authority下的TimeControl类是Lizy模式的，因为在TimeContrlTag类中要用到TimeControl实体
		//所在，这里访问它一次。让它在登陆时，装载到Session中.
//		System.out.println(user.getRoleNames());
		for(Role r : user.getRoleList()){
			for(Authority a :r.getAuthorityList()){
				for(TimeAuthority ta : a.getTimeAuthority()){
					System.out.println(ta.getId());
				}
			}
		}
		userdetails.setLoginName(user.getLoginName());
		userdetails.setName(user.getName());
		userdetails.setPassword(user.getPassword());
		userdetails.setRoleList(user.getRoleList());
		userdetails.setSystemList(user.getSysteList());
		userdetails.setId(user.getId());
		userdetails.setCollegeCode(user.getCollegeCode());
		userdetails.setTree(userDao.getTree(user.getId()));

		return userdetails;
	}

	/**
	 * 获得用户所有角色的权限集合.
	 */
	private Set<GrantedAuthority> obtainGrantedAuthorities(User user) {
		Set<GrantedAuthority> authSet = Sets.newHashSet();
		for (Role role : user.getRoleList()) {
			for (Authority authority : role.getAuthorityList()) {
				authSet.add(new GrantedAuthorityImpl(authority.getPrefixedName()));
			}
		}
		//取消系统层.
//		for(SubSystem ss : user.getSysteList()){
//			for(Role r : ss.getRoleList()){
//				for (Authority authority : r.getAuthorityList()) {
//					authSet.add(new GrantedAuthorityImpl(authority.getPrefixedName()));
//				}
//			}
//		}
		return authSet;
	}

	@Autowired
	public void setAccountManager(AccountManager accountManager) {
		this.accountManager = accountManager;
	}
}
